package com.Mobile.Action;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.Entity.Users;
import com.Mobile.Entity.Emotion;
import com.Service.EmotionService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class SelectEmotionValue implements ServletResponseAware{

	@Autowired
	private EmotionService emotionService;	
	Emotion emotion=new Emotion();	
	private List<Emotion> emotionlist=null;
	private String userid=null;
	private String emotionvalue=null;
	private String creatdate=null;
	private int flag=0;
	private HttpServletResponse response;
	
	public void emotionselect() throws ServletException, IOException{
		
		String userid=ServletActionContext.getRequest().getParameter("userid");
		String datestart=ServletActionContext.getRequest().getParameter("datestart");
		String dateover=ServletActionContext.getRequest().getParameter("dateover");
		
		userid = java.net.URLDecoder.decode(userid,"UTF-8");
		datestart = java.net.URLDecoder.decode(datestart,"UTF-8");
		dateover = java.net.URLDecoder.decode(dateover,"UTF-8");			
		
		JSONObject returnjson=new JSONObject();
		JSONArray jsonarray=new JSONArray();
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json;charset=utf-8"); 
		try {
			PrintWriter out = response.getWriter();
			if(userid!=null&&userid!=null){
				//鐎电懓寮弫鎷岀箻鐞涘矁袙閻?
				flag=1;
				/*SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//鐠佸墽鐤嗛弮銉︽埂閺嶇厧绱?
				try {
					Date datestart1=df.parse(datestart);
					Date dateover2=df.parse(dateover);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				dateover=df.format (dateover);*/
				
				System.out.println(datestart);// new Date()娑撻缚骞忛崣鏍х秼閸撳秶閮寸紒鐔告闂?
				System.out.println(userid);
				System.out.println(dateover);	
				
				int id=Integer.parseInt(userid);
				System.out.println(id);
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("id", id);
				params.put("datestart", datestart);
				params.put("dateover", dateover);
				System.out.println(params);
				String sql="select EmotionValue,CreateDate from emotion where UserId = :id and CreateDate >= :datestart and CreateDate<= :dateover";
				emotionlist=this.emotionService.findBySql(sql, params);
				System.out.println(emotionlist);												
		
				returnjson.put("flag",flag);				
				returnjson.put("emotionlist",emotionlist);
			}
			else
			{
				returnjson.put("flag", flag);
			}
			out.println(returnjson.toString());
			out.flush();
			out.close();
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
		
	public void setServletResponse(HttpServletResponse response) {
		// TODO Auto-generated method stub
		this.response=response;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getEmotionvalue() {
		return emotionvalue;
	}

	public void setEmotionvalue(String emotionvalue) {
		this.emotionvalue = emotionvalue;
	}

	public String getCreatdate() {
		return creatdate;
	}

	public void setCreatdate(String creatdate) {
		this.creatdate = creatdate;
	}

	public EmotionService getEmotionService() {
		return emotionService;
	}

	public void setEmotionService(EmotionService emotionService) {
		this.emotionService = emotionService;
	}

	public Emotion getEmotion() {
		return emotion;
	}

	public void setEmotion(Emotion emotion) {
		this.emotion = emotion;
	}

}
